//
//  ViewController.m
//  CWStarRatingViewDemo
//
//  Created by WANGCHAO on 14/11/8.
//  Copyright (c) 2014年 wangchao. All rights reserved.
//

#import "SendCommtViewC.h"
#import "CWStarRateView.h"
#import "UserModel.h"

@interface SendCommtViewC (){
    
    NSString *star;
}

@property (strong, nonatomic) CWStarRateView *starRateView;

@end

@implementation SendCommtViewC

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"发布评价";
    self.starRateView = [[CWStarRateView alloc] initWithFrame:
                         CGRectMake(10, 0, 100, 40) numberOfStars:5];
    self.starRateView.scorePercent = 1;
    self.starRateView.allowIncompleteStar = YES;
    self.starRateView.hasAnimation = YES;
    [self.strarView addSubview:self.starRateView];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(starNum:)
                                        name:@"NSNotification_STAR"
                                               object:nil];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    star = @"5";
    
}

- (void)starNum:(NSNotification *)noti {
    star = [noti object];
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [_textView resignFirstResponder];
}

- (IBAction)submitBtn:(id)sender{
    
    NSString *text = _textView.text;
    
    if (star.length == 0 || text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请将评价填写完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    else{
        NSString *idNum = [UserModel sharedInstance].currentUser.ID;
        
        [[UserModel sharedInstance] sendCommtWithAccount:idNum sportsID:self.sportId cont:text level:star getSend:^(BOOL successed, unsigned int value) {
            if (successed) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发布评价成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发布评价失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
    }
}



@end
